YZYZ.Admin.Route.relatedWin			=null;

YZYZ.Admin.Route.usedScenicsGrid 	= null;
YZYZ.Admin.Route.usedScenicsStore 	= null;
YZYZ.Admin.Route.usedScenicsCM 		= null;
YZYZ.Admin.Route.usedScenicsPanel	= null;

YZYZ.Admin.Route.scenicsStore 		= null;
YZYZ.Admin.Route.scenicsCM			= null;
YZYZ.Admin.Route.scenicsGrid 		= null;
YZYZ.Admin.Route.scenicsPanel		= null;

YZYZ.Admin.Route.regionPanel 		= null;
YZYZ.Admin.Route.groupStore			= null;
YZYZ.Admin.Route.groupCB 			= null;
YZYZ.Admin.Route.groupForm			= null;
YZYZ.Admin.Route.handleChange=function(cb, newValue, oldValue){
	YZYZ.Admin.Route.scenicsGrid.getStore().setBaseParam("regionId",cb.getValue());
	YZYZ.Admin.Route.scenicsGrid.getStore().setBaseParam("routeId",YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id"));
	YZYZ.Admin.Route.scenicsGrid.getStore().load({params:{start:0,limit:30}});
};
/**
 * 使用的scenic
 */
YZYZ.Admin.Route.usedScenicRecord = Ext.data.Record.create([
	{ name: 'id', type: 'int'},
	{ name: 'name', type: 'string'}
]);
YZYZ.Admin.Route.doUse=function(id){
	var scenicRec = YZYZ.Admin.Route.scenicsGrid.getSelectionModel().getSelected();
	Ext.Ajax.request( {
		url		: Ext.get("useUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:scenicRec.get("id"),
			routeId:YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("操作成功");
				scenicRec.set("used",true);
				YZYZ.Admin.Route.scenicsGrid.getStore().commitChanges();
				var rec = new YZYZ.Admin.Route.usedScenicRecord({
					id:id,
					name:scenicRec.get("name")
				});
				YZYZ.Admin.Route.usedScenicsGrid.getStore().insert(0, rec);
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
			flag = false;
		}
	});

};
/**
 * 在已使用的景区中取消
 */
YZYZ.Admin.Route.doCancel=function(id){
	var scenicRec 	= YZYZ.Admin.Route.scenicsGrid.getSelectionModel().getSelected();
	var routeId 	= YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id");
	Ext.Ajax.request( {
		url		: Ext.get("cancelUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:scenicRec.get("id"),
			routeId:YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("操作成功");
				var count = YZYZ.Admin.Route.usedScenicsGrid.getStore().getCount();
				while(count > 0){
					count--;
					var usedScenicRec = YZYZ.Admin.Route.usedScenicsGrid.getStore().getAt(count);
					if(parseInt(usedScenicRec.get("id"))==parseInt(scenicRec.get("id"))){
						scenicRec.set("used",false);
						YZYZ.Admin.Route.scenicsGrid.getStore().commitChanges();
						YZYZ.Admin.Route.usedScenicsGrid.getStore().remove(usedScenicRec);
						break;
					}
				}
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
			flag = false;
		}
	});
};
/**
 * 在已经使用的景区中取消某个景区
 */
YZYZ.Admin.Route.doCancelForUsed=function(id){
	var scenicRec 	= YZYZ.Admin.Route.usedScenicsGrid.getSelectionModel().getSelected();
	var routeId 	= YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id");
	Ext.Ajax.request( {
		url		: Ext.get("cancelUrl").dom.value,
		timeout	: 30000,
		method	: 'POST',
		params	: {
			scenicId:scenicRec.get("id"),
			routeId:YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id")
		},
		success : function(result, request) {
			result = Ext.util.JSON.decode(result.responseText);
			if(result.code == YZYZ.success){
				YZYZ.Curtain.msg("操作成功");
				var count = YZYZ.Admin.Route.scenicsGrid.getStore().getCount();
				while(count > 0){
					count--;
					var unusedScenicRec = YZYZ.Admin.Route.scenicsGrid.getStore().getAt(count);
					if(parseInt(unusedScenicRec.get("id"))==parseInt(scenicRec.get("id"))){
						unusedScenicRec.set("used",false);
						YZYZ.Admin.Route.scenicsGrid.getStore().commitChanges();
						break;
					}
				}
				YZYZ.Admin.Route.usedScenicsGrid.getStore().remove(scenicRec);
				
			}
			else {
				YZYZ.Curtain.alert(result.message);
				flag = false;
			}
		},
		failure : function(result, request) {
			YZYZ.Curtain.alert("程序出现错误或网络故障");
			flag = false;
		}
	});
};
YZYZ.Admin.Route.opRenderScenic=function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>";
	if(value == false) {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Route.doUse("+record.get('id')+")' class='btnDisabled divDisabled'>使用</button></div>"
	} else {
		html = html + "<div class='divBtn' ><button type='button' onclick='YZYZ.Admin.Route.doCancel("+record.get('id')+")' class='btnDisabled divEnabled'>取消</button></div>"
	}
	html = html + "</div>"
	return html;
};
YZYZ.Admin.Route.opRenderUsedScenic=function(value, metaData, record, rowIndex, colIndex, store){
	var html = "<div class='btnCotainer'>";
	html = html + "<div class='divBtn' ><button type='button'onclick='YZYZ.Admin.Route.doCancelForUsed("+record.get('id')+")' class='btnDisabled divEnabled'>取消</button></div>";
	html = html + "</div>";
	return html;
};
YZYZ.Admin.Route.usedScenicStoreLoad=function(store, records, options){
	if(Ext.isEmpty(YZYZ.Admin.Route.groupCB.getValue()) || isNaN(YZYZ.Admin.Route.groupCB.getValue())){
		return;
	}
	YZYZ.Admin.Route.scenicsStore.setBaseParam("regionId",YZYZ.Admin.Route.groupCB.getValue());
	YZYZ.Admin.Route.scenicsStore.setBaseParam("routeId",YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id"));
	YZYZ.Admin.Route.scenicsStore.load({params:{start:0,limit:30}});
};
YZYZ.Admin.Route.scenicsStoreLoad=function(store, records, options){
	
};
YZYZ.Admin.Route.showRelatedWin=function(){
	if(!YZYZ.Admin.Route.relatedWin){
		YZYZ.Admin.Route.groupStore = new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getRegionsForScenicUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root : "regions"
        	}, [
                {name:'id'},
                {name:'name'}
            ]),
	        remoteSort: false
		});
		YZYZ.Admin.Route.groupStore.load();
		YZYZ.Admin.Route.groupCB = new Ext.form.ComboBox({  
	         id: 'addRegion', 
	         name:'addRegion', 
	         allowBlank: false,  
	         fieldLabel: "请选择地区",  
	         hiddenName: 'hidAddRegion',  
	         displayField: 'name',  
	         valueField: 'id',  
	         typeAhead: true,  
	         triggerAction: 'all',  
	         store: YZYZ.Admin.Route.groupStore,  
	         mode: 'local',
	         width:300 ,
	         editable:false
 		});
 		YZYZ.Admin.Route.groupCB.on("select",YZYZ.Admin.Route.handleChange)
 		YZYZ.Admin.Route.groupForm = new Ext.form.FormPanel({
 			width:400,
 			height:40,
 			labelWidth:100,
 			region:'north',
 			bodyStyle:'padding:10px 0px 0px 100px',
 			labelAlign:'right',
 			items:[YZYZ.Admin.Route.groupCB]
 		});
		/**
		 * used scenics
		 */
		YZYZ.Admin.Route.usedScenicsCM = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 400, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '操作',id:'usedOperations', width: 100, sortable: false, hideable:false, dataIndex: 'ownerId', renderer:YZYZ.Admin.Route.opRenderUsedScenic}
    	]);
    	YZYZ.Admin.Route.usedScenicsStore=new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getUsedUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root			: "data",
	        	totalProperty	: "sum"
        	}, [
                {name:'id'},
                {name:'name'},
                {name:'ownerId',type:'int'}
            ]),
	        remoteSort: false
		});
		YZYZ.Admin.Route.usedScenicsStore.on("load",YZYZ.Admin.Route.usedScenicStoreLoad,this);
		YZYZ.Admin.Route.usedScenicsGrid=new Ext.grid.GridPanel({
			id			: "useScenicGrid",
			store		: YZYZ.Admin.Route.usedScenicsStore,
			cm			: YZYZ.Admin.Route.usedScenicsCM,
			sm			: new Ext.grid.RowSelectionModel(),
			region		: 'center',
			height		: 200,
			width		: 500,
			autoScroll  : true,
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'usedOperations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 30,   
				store		: YZYZ.Admin.Route.usedScenicsStore,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有关联的景区"
			})              
		});
		YZYZ.Admin.Route.usedScenicsPanel=new Ext.Panel({
			collapsible:true,
			title:'关联的景区',
			width:500,
			height:210,
			layout:'border',
			region:'south',
			items:[YZYZ.Admin.Route.usedScenicsGrid]
		});
		
		/**
		 * unused scenics
		 */
		YZYZ.Admin.Route.scenicsCM = new Ext.grid.ColumnModel([
			new Ext.grid.RowNumberer(),
            {header: '名称', width: 400, sortable: false, hideable:false, dataIndex: 'name'},
            {header: '操作',id:'scenicsOperations', width: 100, sortable: false, hideable:false, dataIndex: 'used', renderer:YZYZ.Admin.Route.opRenderScenic}
    	]);
    	YZYZ.Admin.Route.scenicsStore=new Ext.data.Store({  
			proxy: new Ext.data.HttpProxy ({
	            url: document.getElementById("getScenicsForRouteUrl").value
	        }),
	        reader: new Ext.data.JsonReader({
	        	root			: "data",
	        	totalProperty	: "sum"
        	}, [
                {name:'id'},
                {name:'name'},
                {name:'used',type:'boolean'}
            ]),
	        remoteSort: false
		});
		YZYZ.Admin.Route.scenicsStore.on("load",YZYZ.Admin.Route.scenicsStoreLoad);
		YZYZ.Admin.Route.scenicsGrid=new Ext.grid.GridPanel({
			id			: "scenicGrid",
			store		: YZYZ.Admin.Route.scenicsStore,
			cm			: YZYZ.Admin.Route.scenicsCM,
			sm			: new Ext.grid.RowSelectionModel(),
			region		: 'center',
			height		: 200,
			width		: 500,
			autoScroll  : true,
			viewConfig	: { forceFit:true },
			autoExpandColumn : 'scenicsOperations',
			loadMask	: { msg:"数据加载中....." },
			bbar		: new Ext.PagingToolbar({   
				pageSize	: 30,   
				store		: YZYZ.Admin.Route.scenicsStore,   
				displayInfo	: true,   
				displayMsg	: "第{0}条到{1}条  共{2}条",   
				emptyMsg	: "没有景区"
			})              
		});
		YZYZ.Admin.Route.scenicsPanel=new Ext.Panel({
			collapsible:true,
			title:'景区',
			width:500,
			height:300,
			layout:'border',
			region:'center',
			items:[YZYZ.Admin.Route.scenicsGrid,YZYZ.Admin.Route.groupForm]
		});
		
		YZYZ.Admin.Route.relatedWin=new Ext.Window({
			title:'景区设置',
			layout:'border',
			items:[YZYZ.Admin.Route.scenicsPanel,YZYZ.Admin.Route.usedScenicsPanel],
			closable:true,
			closeAction:'hide',
			width:600,
			height:500,
			modal:true,
			buttons:[{
				text:'关闭',
				width:60,
				handler:function(){YZYZ.Admin.Route.relatedWin.hide();}
			}]
		})
	}

	YZYZ.Admin.Route.relatedWin.show();
	YZYZ.Admin.Route.usedScenicsStore.setBaseParam("routeId",YZYZ.Admin.Route.grid.getSelectionModel().getSelected().get("id"));
	YZYZ.Admin.Route.usedScenicsGrid.getStore().load();
};